//
//  ZooNetwork.m
//  ZooNgonDoiOS
//
//  Created by ViệtBQ on 5/29/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ZooNetwork.h"
#import "RestKit/RestKit.h"
#import "ZooAuthBean.h"
#import "ZooLogin.h"
#import "ZooAuth.h"
#import "ZooUtil.h"
#import "ZooLoginResponse.h"

@implementation ZooNetwork
@synthesize rkObjectManager = _rkObjectManager;
@synthesize isAuth;

static ZooNetwork *_shareZooNetwork = nil;
static NSString *CONSUMER_KEY = @"0a9fd3a81e14b1ca6e9a60ba6751488878c66737";
static NSString *CONSUMER_SECRET = @"99e179f81ae8c8613ef840e0d36d68f3c9fab11b";
static NSString *OAUTH_URL = @"/auth/request_token";
static NSString *LOGIN_URL = @"/login";

-(id) init{
    self = [super init];
    if (self) {
        
        _accessToken = [ZooUtil LoadTokenValue];
        _accessTokenSecret = [ZooUtil LoadTokenSecretValue];
        
        RKObjectManager *sharedManager = [RKObjectManager objectManagerWithBaseURLString:@"http://api.ngon.do"];
        // Enable automatic network activity indicator management
        sharedManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
        sharedManager.client.authenticationType = RKRequestAuthenticationTypeOAuth1;
        
        sharedManager.client.OAuth1ConsumerKey = CONSUMER_KEY;
        sharedManager.client.OAuth1ConsumerSecret = CONSUMER_SECRET;
        sharedManager.client.OAuth1AccessToken = _accessToken;
        sharedManager.client.OAuth1AccessTokenSecret = _accessTokenSecret;
        
        if (nil == _shareZooNetwork) {
            [ZooNetwork setSharedNetwork:self];
        }
        
        if(_accessToken == nil || _accessTokenSecret == nil){
            isAuth = false;
        }else{
            isAuth = true;
        }
    }
    return self;
}

+ (void)setSharedNetwork:(ZooNetwork *) network {
    [network retain];
    [_shareZooNetwork release];
    _shareZooNetwork = network;
}

+ (ZooNetwork *)sharedNetwork {
    return _shareZooNetwork;
}

-(void) sendRequestOAuth{
    
    ZooAuth *zooAuth = [[ZooAuth alloc]initWithTarget:self selector:@selector(onAuthOK:tokenSecret:)];
    
    RKObjectMapping *zooAuthMapping = [RKObjectMapping mappingForClass:[ZooAuthBean class]];
    
    [zooAuthMapping mapKeyPath:@"token" toAttribute:@"_token"];
    [zooAuthMapping mapKeyPath:@"token_secret" toAttribute:@"_token_secret"];
    [zooAuthMapping mapKeyPath:@"status" toAttribute:@"_status"];

    [[RKObjectManager sharedManager].mappingProvider setObjectMapping:zooAuthMapping forKeyPath:@""];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:OAUTH_URL delegate:zooAuth];
}

- (void) requestLogin:(NSString *)username password:(NSString *)password {
//    _delegateLogin = delegate;
//    [RKObjectManager sharedManager] setValue:<#(id)#> forKey:<#(NSString *)#>
//    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:LOGIN_URL delegate:<#(id<RKObjectLoaderDelegate>)#>];
    ZooLogin *zooLogin = [[ZooLogin alloc] initWithTarget:self selector:@selector(onLoginOK:)];
    NSDictionary *loginParams = [NSDictionary dictionaryWithKeysAndObjects:@"username", username,
                                 @"password", password, 
                                 @"token_key", _accessToken, nil];

    [[RKObjectManager sharedManager].client get:LOGIN_URL queryParameters:loginParams delegate:zooLogin];

}
    
- (void)sendRequest {
    
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    RKLogError(@"Load of RKRequest %@ failed with error: %@", request, error);
    NSLog(@"abc");
    [request release];
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    
}

- (void) objectLoader:(RKObjectLoader *)objectLoader didLoadObject:(id)object{
    
}

-(void) onAuthOK:(NSString *) token tokenSecret:(NSString *)token_secret{
    isAuth = true;
    NSLog(@"Auth token = %@ | token_secret = %@",token,token_secret);
    [RKObjectManager sharedManager].client.OAuth1AccessToken = token;
    [RKObjectManager sharedManager].client.OAuth1AccessTokenSecret = token_secret;
    _accessToken = token;
    [ZooUtil SaveTokenKey:token tokenSecretValue:token_secret];
}

-(void) onAuthErrorWithMessage:(NSString *) mess{
    
}


-(void) onLoginOK:(ZooLoginResponse *)pZooLoginResponse{
    NSLog(@"User name=%@", pZooLoginResponse.userName);
}

-(void) onLoginFail: (NSString *) mess {
    NSLog(@"Login fail");
}
@end
